package com.dong.service.Provider;

import com.dong.dao.BaseDao;
import com.dong.dao.Bill.BillDao;
import com.dong.dao.Bill.BillDaoImpl;
import com.dong.dao.Provider.ProviderDao;
import com.dong.dao.Provider.ProviderDaoImpl;
import com.dong.pojo.Provider;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class ProviderServiceImpl implements ProviderService{
    //业务层都会调用Dao层，所以我们需要引入Dao层
    private ProviderDao providerDao;
    private BillDao billDao;
    public ProviderServiceImpl() {
        providerDao = new ProviderDaoImpl();
        billDao = new BillDaoImpl();
    }


    public boolean add(Provider provider) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);
            int updateRows = providerDao.add(connection, provider);
            connection.commit();
            if(updateRows > 0){
                flag = true;
                System.out.println("addprovider success!");
            }else{
                System.out.println("addprovider failed!");
            }
        } catch (SQLException throwables) {
            try {
                System.out.println("rollback==================");
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    public List<Provider> getProviderList(String proName, String proid) {
        Connection connection = null;
        List<Provider> providerList = null;
        System.out.println("query proName ---- > " + proName);
        System.out.println("query proCode ---- > " + proid);
        try {
            connection = BaseDao.getConnection();
            providerList = providerDao.getProviderList(connection, proName,proid);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return providerList;
    }

    public List<Provider> getProviderList(String proName, String proCode,int currentPageNo, int pageSize) {
        Connection connection = null;
        List<Provider> providerList = null;
        System.out.println("query proName ---- > " + proName);
        System.out.println("query proCode ---- > " + proCode);
        System.out.println("currentPageNo-->"+currentPageNo);
        System.out.println("pageSize-->"+pageSize);

        try {
            connection = BaseDao.getConnection();
            providerList = providerDao.getProviderList(connection, proName, proCode,currentPageNo,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return providerList;
    }

    //通过供应商id查找供应商
    public Provider getProviderById(String id) {
        Connection connection = null;
        Provider provider = null;
        try {
            connection = BaseDao.getConnection();
            provider = providerDao.getproviderByid(connection, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            provider = null;
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return provider;
    }

    public boolean modifyProvider(Provider provider) {
        boolean flag = false;
        Connection connection = null;

        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);//开启JDBC事务
            int updateRows = providerDao.modifyProvider(connection, provider);
            connection.commit();
            if( updateRows > 0){
                flag = true;
                System.out.println("modify success!");
            }else{
                System.out.println("modify failed!");
            }
        } catch (SQLException throwables) {
            try {
                System.out.println("rollback==================");
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    public int deleteProviderById(String delId) {
        Connection connection = null;
        int billCount = -1;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);
            billCount = billDao.getBillCountByProviderId(connection,delId);
            if(billCount == 0){
                providerDao.deleteProviderById(connection,delId);
            }
        } catch (Exception throwables) {
            billCount = -1;
            try {
                System.out.println("rollback ========");
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return billCount;
    }

    public int getUserCount(String proName, String proCode) {
        Connection connection = null;
        int count = 0;
        try {
            connection = BaseDao.getConnection();
            count = providerDao.getProviderCount(connection,proName,proCode);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return count;
    }

    @Test
    public void Test(){

    }
}
